Tangible Software Solutions

C# to Java Converter Help


Contents

  1. Project & Folder Conversion
  2. File & Snippet Conversion
  3. Command Line Conversion
  4. Code Formatting Options
  5. Conversion Message Options
  6. Custom String Replacements
  7. Type & Member Replacements
  8. Diagnostic Options
  9. File Encoding Options
  10. Additions & Omissions
  11. Project Conversion Groups
  12. Default Shared Assemblies for Conversions
  13. Assumed Imported Namespaces for Snippet Conversions
  14. Package & File Options
  15. Property Conversion Options
  16. Naming Conventions
  17. .NET to Java Class Library Options
  18. Omit Types from Conversion
  19. Miscellaneous Options
  20. Assembly Resolution
  21. Conversion of 'ref' Parameters
  22. Conversion of Lambdas and Delegates


Project & Folder Conversion

The Project & Folder Converter tab allows you to convert all C# code within a project or folder. Once the files are converted, you can then view C# to Java Converter comments, compare converted code files to original code files, and view the converted code folder.

The Conversion History listview displays previously converted sources and allows you to view remaining C# to Java Converter comments and view the converted code folder.

Note that due to the lack of similarity between C# and Java project types and project files, C# to Java Converter does not generate Java project files. You need to assemble the resulting .java files generated by C# to Java Converter into a new Java project.


File & Snippet Conversion

The File & Snippet Converter tab allows you to convert C# code snippets or files. From the left side textbox, you can type or paste code snippets, insert files, or view C# to Java Converter sample snippets. Once a file or snippet is converted, you can select and copy the results, save the results to a file, or search the results.


Command Line Conversion

C# to Java Converter can be launched from the command line with two or three parameters. If only one parameter is specified, it is interpreted as the name of one of your custom project conversion groups. If two parameters are specified, the first parameter is a C# project file, folder, or source code file that you wish to convert, and the second parameter is the folder that the converted Java files will be written to or the name of the converted file for the single file conversion case. If the optional last parameter indicating the location of a conversion options file is included, then that file is imported before the conversion starts. A conversion options file has the extension '.opt' and is produced by exporting existing options to an xml file from the converter.

e.g. (project group conversion),
"CSharp to Java Converter" "MyProjectGroup"

e.g. (single project conversion),
"CSharp to Java Converter" "C:\C# Projects\Sample Project.csproj" "C:\Java\Sample Target Folder"

e.g. (folder conversion),
"CSharp to Java Converter" "C:\C# Projects\Sample Source Folder" "C:\Java\Sample Target Folder"

e.g. (single file conversion),
"CSharp to Java Converter" "C:\C#\testfile.cs" "C:\Java\testfile.java"

e.g. (conversion of a list of files - one file per line),
"CSharp to Java Converter" "C:\list-of-files.txt" "C:\Java\Sample Target Folder"

e.g. (folder conversion with a conversion options file specified),
"CSharp to Java Converter" "C:\C# Projects\Sample Source Folder" "C:\Java\Sample Target Folder" "C:\Options\C# to Java Converter.opt"

Once the command line conversion is complete, C# to Java Converter will shut down.

Note that 'CSharp to Java Converter' must be in double quotes since the executable name contains a space.


Code Formatting Options


Conversion Message Options

On this options dialog, you can customize the comment prefixes that the converter uses for messages and also toggle the messages that C# to Java Converter displays on or off.

You can easily set all or none to display via the 'Select All' and 'Select None' buttons.


Custom String Replacements

C# to Java Converter allows you to specify your own custom replacements in the converted Java code. This can be useful for customizing code according to style preferences that may be different in Java than in C#. It's also useful for cases where a .NET method without a Java equivalent will be replaced by your own custom method to be called from the converted code.

For each replacement string you can specify the regex option, when to perform the replacement (pre or post conversion), whether or not to match case, whether to match whole words only, and whether comments or string literals should be modified. The whole words and string literals options do not apply if the regex option is selected.

To span mulitple lines, either paste directly into the 'Find' and 'Replacement' textboxes, or use the escaped characters: \r\n\t.

The "Active" checkbox can be unchecked if you wish to temporarily bypass the replacement.

Replacements are made in the order that they appear on the options dialog. For example, the second replacement is made to each line of code after the first replacement has already been made. To change the order of replacements, use the 'Up' and 'Down' buttons.

For an example of the regex option, you could do a custom replacement of "Foo(x)" with "Bar(x)", where "x" is any argument, by specifying the "Find" field as "Foo(([\sa-zA-Z_$]*))" and the "Replace" field as "Bar$1". Note that there are differences in various regex engines - the regex engine used by the converter is .NET System.Text.RegularExpressions.Regex.


Type & Member Replacements

C# to Java Converter allows you to specify your own custom type and member replacements in the converted code. This can be useful for cases where you wish to specify replacements for .NET library member calls of a specific type that are not handled by C# to Java Converter.

For each external type, you can specify an optional type replacement and multiple member replacements. For types, you can specify generic types, which may include '?' as a wildcard type. A wildcard in the replacement type must have a matching wildcard in the same position in the original type.

e.g., the following will replace all references to type 'Foo' having one 'int' type parameter with 'Bar', having no type parameters:
original type: Foo<int>
replacement type: Bar

e.g., the following will replace all references to type 'Foo' having one type parameter of any type with 'Bar', having the same type parameter:
original type: Foo<?>
replacement type: Bar<?>

For each member replacement, you specify the original member name, whether the member is static or not, and whether the member is a method or a field/property. You also specify the number of arguments you expect. To replace C# operator invocations, specify the original method as static and use "operator+", "operator-", etc. for the original method name.

For the replacement member, you specify whether the replacement is static or not, and whether the replacement is a method or a field. You also specify the new list of arguments and can add literal arguments. For replacement of static methods with instance members, you also specify the argument position which corresponds to the object instance for the new instance member. For replacement of instance members with static members, you specify the new class name used to call the static member (for static methods, you also specify the position in the argument list to insert the previous instance qualifier, using the string "instance" (quotes not included) at the appropriate position in the "new argument list" field). For example, if you want to replace all instance calls to the 'Foo' type method 'Bar' where you want the instance qualifier to be the new first argument, you would specify "instance, a1" (quotes not included) for the "new argument list" field, assuming that there was one argument in the original method calls.


Diagnostic Options

These options are intended to provide information to Tangible Software Solutions if a conversion is taking longer than expected. 'Record processing by file' instructs the converter to make a record of the files processed and the time taken for each process. 'Record the last 100 lines of code parsed before cancelling conversion' instructs the converter to make a record of the most recent lines parsed before you cancelled the conversion.


File Encoding Options

The converter detects input file encoding from the byte order mark (BOM), but if the BOM is missing the converter assumes by default that the file encoding is UTF-8. You can use this option to specify a different default encoding to use in the absence of a BOM. Output is written in the same encoding as the encoding detected for the original file, or your default encoding option if the original coding was not detected via the BOM. For UTF-8, you can choose whether or not to include the BOM for the output - this is normally used in Windows environments.


Additions & Omissions


Project Conversion Groups

If you need to regularly convert many projects at the same time, use the Conversion Groups feature. This is most commonly used to convert sets of sample projects to Java.

To configure Conversion Groups, go to Options->Project Conversion Groups on the main menu or toolbar.
If you enter a folder for "common folder for added helper classes", the converter will write all helper class files to this location. If nothing is entered here, the helper class files will be written for each project in the group separately.

To convert a Conversion Group, go to Convert->Convert Group on the main menu.


Default Shared Assemblies for Conversions

For snippet and folder conversions, you can specify the assemblies that the converter should resolve when interpreting the original code.

For .NET Core SDK projects, the project file contains no information about the shared assemblies used by the project. The converter will not automatically attempt to resolve every assembly in the .NET Core shared assembly location since it takes time to resolve the type information in each assembly. Instead, we resolve only the shared assembly defining the core types (System.Private.CoreLib) and allow you to select other assemblies for each project SDK that you typically use.

For .NET Framework SDK projects, the project file contains no information about the GAC assemblies used by the project. The converter will not automatically attempt to resolve every assembly in the .NET Framework GAC assembly location since it takes time to resolve the type information in each assembly. Instead, we resolve only the GAC assemblies 'mscorlib' and 'System' and allow you to select other assemblies for each project SDK that you typically use.


Assumed Imported Namespaces for Snippet Conversions

You can specify namespaces that you would like the snippet converter to assume are available when interpreting the original code. The snippet converter will then convert as if the snippet contained the corresponding 'using' directives.


Package & File Options


Property Conversion Options


Naming Conventions

By default, the converter does not change naming conventions, but you can select these options to cause the converter to modify names to conform to these common Java conventions.


.NET to Java Class Library Options


Omit Types from Conversion

Any type entered here will be omitted from most conversion logic. You would normally enter library type names here to prevent the default conversion of objects of those types.


Miscellaneous Options


Conversion of 'ref' Parameters

Java has no equivalent to C#'s ref parameters, but C# to Java Converter uses the helper class RefObject to provide a conversion which reproduces the same behavior.

e.g., the following C# method:
public void Test(ref int i)

is converted to:
public void Test(tangible.RefObject<Integer> i)

and a call to this method is converted to:
tangible.RefObject<Integer> tempRef_i = new tangible.RefObject<Integer>(i);
Test(tempRef_i);
i = tempRef_i.argValue;

The definition of RefObject is inserted into the converted code. RefObject is defined as follows:

public final class RefObject<T>
{
    public T argValue;
    public RefObject(T refArg)
    {
        argValue = refArg;
    }
}


Conversion of Lambdas and Delegates

Most C# delegates are converted to interfaces and C# lambdas are converted to Java 8 lambdas. If you turn off the option to convert to Java lambdas (see the Miscellaneous Options dialog), then lambdas are converted to anonymous inner classes.

For example, the following C# code:

public delegate void myVoidDelegate(int i);
public myVoidDelegate voidDelegateVar;

private void Test()
{
    voidDelegateVar = new myVoidDelegate(instanceMethod);

    voidDelegateVar = (i) => instanceMethod(i);
}

is converted to the following Java code:

@FunctionalInterface
public interface myVoidDelegate
{
    void invoke(int i);
}
public myVoidDelegate voidDelegateVar;

private void Test()
{
    voidDelegateVar = (int i) -> instanceMethod(i);

    voidDelegateVar = (int i) -> instanceMethod(i);
}

but if you don't select the option to convert to Java 8 lambdas, the 'Test' method will convert to:

private void Test()
{
    voidDelegateVar = new myVoidDelegate()
    {
        @Override
        public void invoke(int i)
        {
            instanceMethod(i);
        }
    };

    voidDelegateVar = new myVoidDelegate()
    {
        @Override
        public void invoke(int i)
        {
            instanceMethod(i);
        }
    };
}


Assembly Resolution

Assemblies referenced by projects are examined to determine the nature of the types and members within the assembly to improve the conversion quality.

During the project conversion process you may be prompted by the converter to find specific assemblies referenced by your project if they cannot otherwise be found by the converter.

If the converter does not resolve all assemblies, you can still continue the project conversion, but the conversion may not be optimal for code which references the unresolved assemblies. This just means that a few more manual adjustments may be required.

If the assembly does not require an install, you can forward it to us in a zip file to determine why it is not resolving.


Copyright © Tangible Software Solutions, Inc.